//引入express模块
var express = require('express');
//创建APP
var app = express();
//创建Server
var server=require('http').createServer(app);
//引入body-parser用来接收post请求的数据
var bodyParser=require('body-parser');
//引入MD5加密模块
var md5=require('./md5');
//引入用户数据源
var users=require('./datasource/user');
//引入PATH
var path=require('path');
//解析POST数据
app.use(bodyParser.urlencoded({extended:true}))

//托管静态文件
app.use(express.static(__dirname+'/assets'));
//网站的入口
app.get('/',(req,res) => {
  res.sendFile(__dirname+'/assets/pages/index.html');
})

//登录页的入口
app.get('/login.html',(req,res) =>{
  res.sendFile(__dirname+'/assets/pages/login.html');
})

//登录
app.post('/login.do',(req,res) =>{
  let username = req.body.username;
  let password = md5(req.body.password);

  if(!username || !password){
    res.send('fail');
    return;
  }

  for(u of users){
    if(u.username === username && u.password=== password && u.state==1){
      res.send('success');
      return;
    }
  }
  res.send('fail');
})
//查询用户
app.get('/users.do',(req,res)=>{
  res.send(users);
})
server.listen(8000);
console.log('Hotel Manage Startup Success in 8000');
